﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Web.SessionState;

namespace wxx.ashx
{
    /// <summary>
    /// 生成代码时间： ljxtime
    /// 文件说明：t_order
    /// 开发者：
    /// 最后编辑时间：
    /// 编辑说明：
    /// </summary>
    public class order : baseashxClass
    {
        
        public override void ProcessRequest(HttpContext context)
        {
            wxx.BLL.t_order bll = new wxx.BLL.t_order(context); 
            base.ProcessRequest(context);
            context.Response.ContentType = "text/plain";

            mycommonClass mycommonClassobj = new mycommonClass();
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            if (context.Request.QueryString["type"] == "edit")//获取编辑信息
            {
                if (context.Request.QueryString["Id"] != null)
                {
                    string strret = bll.Geteditdata(context.Request.QueryString["Id"].ToString());//aa1
                    context.Response.Write(strret);
                }
            }
            else if (context.Request.QueryString["type"] == "del")//删除信息
            {
                if (context.Request.QueryString["Id"] == null)
                    return;
                string Id = context.Request.QueryString["Id"].ToString();                
                bll.Delete(int.Parse(Id));               
            }
            else if (context.Request.QueryString["type"] == "combox")//保存修改或添加 信息 aa2
            {
                        if (context.Request.QueryString["comboxname"] == "ReaderId")
                        {
                            wxx.BLL.t_readerinfo bllzd = new wxx.BLL.t_readerinfo();
                            string strret = bllzd.GetListByColumn_tojson("Id,ReaderId,ReaderId,Name","1=1","");
                            context.Response.Write(strret);
                        }

                        if (context.Request.QueryString["comboxname"] == "ISBN")
                        {
                            wxx.BLL.t_bookinfo bllzd = new wxx.BLL.t_bookinfo();
                            string strret = bllzd.GetListByColumn_tojson("Id,ISBN,BookName,ISBN","1=1","");
                            context.Response.Write(strret);
                        }


            }
             
            else if (context.Request.QueryString["type"] == "save")//保存修改或添加 
            {
                    string ReaderId = context.Request.QueryString["ReaderId"];
                    string ISBN = context.Request.QueryString["ISBN"];
                    string BookName = context.Request.QueryString["BookName"];
                    string OrderTime = DateTime.Now.ToString();
                    string State = context.Request.QueryString["State"];
                    wxx.BLL.t_bookinfo bibll = new BLL.t_bookinfo();
                    wxx.Model.t_bookinfo bimodel = new Model.t_bookinfo();
                    wxx.BLL.t_librarybookinfo lbibll = new BLL.t_librarybookinfo();
                    wxx.Model.t_librarybookinfo lbimodel = new Model.t_librarybookinfo();
                    DataSet bids = bibll.GetList("ISBN='"+ISBN+"'");
                if(bids.Tables[0].Rows.Count>0)
                    BookName = bids.Tables[0].Rows[0]["BookName"].ToString();
                if (context.Request.QueryString["Id"] != null && context.Request.QueryString["Id"] != "")
                {//将图书信息中的书本状态变成未借出
                    DataSet lbids = lbibll.GetList("ISBN='"+ISBN+"'");
                    if (lbids.Tables[0].Rows.Count > 0) {
                        String Idlbi = lbids.Tables[0].Rows[0][0].ToString();
                        lbimodel = lbibll.GetModel(int.Parse(Idlbi));
                        lbimodel.BookState = "未借出";
                        lbibll.Update1(lbimodel);
                    }



                    bll.Update(context.Request.QueryString["Id"].ToString(),ReaderId,ISBN,BookName,OrderTime,State);
                }
                else
                {
                    bll.Add(ReaderId,ISBN,BookName,OrderTime,State);
                }

                context.Response.Write("true");
            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,ISBN,BookName,OrderTime,State", pagestrif, "Id desc");
                context.Response.Write(strret);
                return;
            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query1")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,ISBN,BookName,OrderTime,State","State="+"'已预约'"+"or State="+"'可借阅'", "Id desc");
                context.Response.Write(strret);
                return;
            }
          
           else if (context.Request.QueryString["type"] == "order") {
               if (context.Request.QueryString["ReaderId"] != null && context.Request.QueryString["Id"] != null)
               {

                   String ReaderId = context.Request.QueryString["ReaderId"].ToString();
                   String Id = context.Request.QueryString["Id"].ToString();
                   wxx.Model.t_order omodel = new Model.t_order();
                   wxx.BLL.t_order obll = new BLL.t_order();

                   wxx.BLL.t_bookinfo bibll = new BLL.t_bookinfo();
                   wxx.Model.t_bookinfo bimodel = new Model.t_bookinfo();
                   bimodel = bibll.GetModel(int.Parse(Id));
                   String Num = bimodel.Beizhu.ToString();
                   String ISBN = bimodel.ISBN.ToString();
                   String BookName = bimodel.BookName.ToString();
                   if (int.Parse(bimodel.Beizhu) > 0)
                   {
                       context.Response.Write("notorder");
                   }//库存充足不需要重复预约
                   else
                   {


                       //去借阅表中查看当前这个人的借阅情况
                       wxx.BLL.t_lendsend ldbll = new wxx.BLL.t_lendsend();
                       wxx.BLL.t_librarybookinfo lbibll = new wxx.BLL.t_librarybookinfo();
                       wxx.Model.t_librarybookinfo lbimodel = new wxx.Model.t_librarybookinfo();
                       DataSet ldds = ldbll.GetList("ReaderId='" + ReaderId + "'" + "and BookState=" + "'未归还'");

                       String ldISBN = "";
                       for (int i = 0; i < ldds.Tables[0].Rows.Count; i++)
                       {
                           String ldBarCodeId = ldds.Tables[0].Rows[i][2].ToString();
                           DataSet lbids = lbibll.GetList("BarCodeId='" + ldBarCodeId + "'");
                           if (lbids.Tables[0].Rows.Count > 0)
                               ldISBN = lbids.Tables[0].Rows[0][1].ToString();
                           if (ldISBN == ISBN) break;
                       }
                       if (ldISBN == ISBN)
                       {//避免这个人借阅了书籍又再次预约
                           context.Response.Write("yijieyue");
                       }
                       else
                       {

                           DataSet ods = obll.GetList("ReaderId='" + ReaderId + "'" + "and ISBN='" + ISBN + "'" + "and State=" + "'已预约'");
                           DataSet allds = obll.GetList("ReaderId='" + ReaderId + "'" + "and State=" + "'已预约'");
                           if (allds.Tables[0].Rows.Count < 5)
                           {
                               if (ods.Tables[0].Rows.Count > 0) { context.Response.Write("yiorder"); }
                               else
                               {
                                   omodel.ReaderId = ReaderId;
                                   omodel.ISBN = ISBN;
                                   omodel.BookName = BookName;
                                   omodel.OrderTime = DateTime.Now;
                                   omodel.State = "已预约";
                                   obll.Add1(omodel);
                                   context.Response.Write("true");
                               }
                           }
                           else
                           {
                               context.Response.Write("chaoNum");
                           }
                       }
                   }
               }
            
            }
            else
            {

            }
        }         
    }
}